<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use App\Models\Admin\Stats\PlayerBet\Hour as StatsHour;
use App\Models\Admin\Stats\PlayerBet\Day as StatsDay;
use App\Models\Admin\Stats\PlayerBet\Month as StatsMonth;
use App\Models\Admin\StatsTask\PlayerBet\Hour as TaskHour;
use App\Models\Admin\StatsTask\PlayerBet\Day as TaskDay;
use App\Models\Admin\StatsTask\PlayerBet\Month as TaskMonth;
use App\Core\Traits\Numeral;

class CreateStatsPlayerBetTable extends Migration
{
    use Numeral;

    /**
     * 统计任务表字段
     */
    private function makeStatsTask($tableName)
    {
        return function (Blueprint $table) use ($tableName) {
            $table->id();
            $table->unsignedInteger('timed');
            $table->string('plat_code', 64)->comment('平台code');
            $table->string('game_id', 32)->comment('游戏code');
            $table->integer('scene_id')->comment('场景ID');

            $table->tinyInteger('attempts')->comment('任务尝试次数')->default(0);
            $table->tinyInteger('status')->comment('状态：1初始化，2推送到队列，3开始处理，4成功，5失败')->default(1)->index();

            $table->unsignedInteger('created');
            $table->unsignedInteger('updated');

            // 唯一索引
            $table->unique(['timed', 'plat_code', 'game_id', 'scene_id'], "{$tableName}_unique");
        };
    }

    /**
     * 统计数据表字段
     */
    private function makeStats($tableName)
    {
        return function (Blueprint $table) use ($tableName) {
            $table->id();
            $table->unsignedInteger('timed');
            $table->string('plat_code', 64)->comment('平台code')->index();
            $table->string('game_id', 32)->comment('游戏code')->index();
            $table->integer('scene_id')->comment('场景ID')->index();

            $table->foreignId('uid')->comment('玩家ID')->index();
            $table->string('currency', 16)->comment('币种code')->index();

            $table->string('line_code', 128)->comment('line_code')->index();
            $table->string('account_id', 64)->comment('玩家账号')->index();

            $table->integer('doc_count')->comment('记录条数')->default(0);

            $table->decimal('bet_base', $this->decimals(), $this->precision())->comment('底分')->default(0);
            $table->decimal('bet_num', $this->decimals(), $this->precision())->comment('玩家投注')->default(0);
            $table->decimal('gain_player', $this->decimals(), $this->precision())->comment('玩家盈亏')->default(0);
            $table->decimal('amount', $this->decimals(), $this->precision())->comment('平台收益')->default(0);

            $table->json('usd')->comment('换算USD');

            $table->unsignedInteger('created');
            $table->unsignedInteger('updated');

            // 唯一索引
            $table->unique(['timed', 'plat_code', 'game_id', 'scene_id', 'uid', 'currency'], "{$tableName}_unique");
        };
    }

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        /**
         * 统计任务表【小时】
         */
        $tableName = TaskHour::tableName();
        // 创建表结构
        TaskHour::schemaConnection()->create($tableName, $this->makeStatsTask($tableName));
        // 表名注释
        TaskHour::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计任务--玩家投注--小时'");
        /**
         * 统计数据表【小时】
         */
        $tableName = StatsHour::tableName();
        // 创建表结构
        StatsHour::schemaConnection()->create($tableName, $this->makeStats($tableName));
        // 表名注释
        StatsHour::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计--玩家投注--小时'");

        /**
         * 统计任务表【天】
         */
        $tableName = TaskDay::tableName();
        // 创建表结构
        TaskDay::schemaConnection()->create($tableName, $this->makeStatsTask($tableName));
        // 表名注释
        TaskDay::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计任务--玩家投注--天'");
        /**
         * 统计数据表【天】
         */
        $tableName = StatsDay::tableName();
        // 创建表结构
        StatsDay::schemaConnection()->create($tableName, $this->makeStats($tableName));
        // 表名注释
        StatsDay::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计--玩家投注--天'");

        /**
         * 统计任务表【月】
         */
        $tableName = TaskMonth::tableName();
        // 创建表结构
        TaskMonth::schemaConnection()->create($tableName, $this->makeStatsTask($tableName));
        // 表名注释
        TaskMonth::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计任务--玩家投注--月'");
        /**
         * 统计数据表【月】
         */
        $tableName = StatsMonth::tableName();
        // 创建表结构
        StatsMonth::schemaConnection()->create($tableName, $this->makeStats($tableName));
        // 表名注释
        StatsMonth::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计--玩家投注--月'");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // 统计任务表【小时】
        TaskHour::schemaConnection()->dropIfExists(TaskHour::tableName());
        // 统计数据表【小时】
        StatsHour::schemaConnection()->dropIfExists(StatsHour::tableName());

        // 统计任务表【天】
        TaskDay::schemaConnection()->dropIfExists(TaskDay::tableName());
        // 统计数据表【天】
        StatsDay::schemaConnection()->dropIfExists(StatsDay::tableName());

        // 统计任务表【月】
        TaskMonth::schemaConnection()->dropIfExists(TaskMonth::tableName());
        // 统计数据表【月】
        StatsMonth::schemaConnection()->dropIfExists(StatsMonth::tableName());
    }
}
